package com.sztx.pay.center.rpc.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.Constance.FinanceOrderState;
import com.sztx.pay.center.common.util.Constance.FinanceTradeOrderType;
import com.sztx.pay.center.common.util.Constance.PayFlowType;
import com.sztx.pay.center.common.util.Constance.TradeOrderType;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.DateUtil;
import com.sztx.pay.center.common.util.StringUtil;
import com.sztx.pay.center.common.yhbf.YHBFSensitiveEnc;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.dataaccess.dao.hbase.DumpOrderHbaseDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.FinanceStatisticsMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.GatewayChannelMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowCollectMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskTradeTypeMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.FinanceStatisticsDO;
import com.sztx.pay.center.dataaccess.domain.GatewayChannelDO;
import com.sztx.pay.center.dataaccess.domain.HbaseQueryDO;
import com.sztx.pay.center.dataaccess.domain.PayFlowCollectDO;
import com.sztx.pay.center.dataaccess.domain.PlatformTradeFlowDO;
import com.sztx.pay.center.dataaccess.domain.RiskTradeTypeDO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserActFlowDumpDO;
import com.sztx.pay.center.rpc.api.domain.FinanceCollectResponse;
import com.sztx.pay.center.rpc.api.domain.PageModelResponse;
import com.sztx.pay.center.rpc.api.domain.PlatformTradeFlowHbaseResp;
import com.sztx.pay.center.rpc.api.domain.PlatformTradeFlowResponse;
import com.sztx.pay.center.rpc.api.domain.TradeFlowListResponse;
import com.sztx.pay.center.rpc.api.domain.TradeFlowResponse;
import com.sztx.pay.center.rpc.api.domain.TradeOrderListResponse;
import com.sztx.pay.center.rpc.api.domain.TradeOrderRequest;
import com.sztx.pay.center.rpc.api.domain.TradeOrderResponse;
import com.sztx.pay.center.rpc.api.domain.TypeCodeNameResponse;
import com.sztx.pay.center.rpc.api.domain.request.PlatformTradeFlowHbaseRequest;
import com.sztx.pay.center.rpc.api.domain.request.PlatformTradeFlowRequest;
import com.sztx.pay.center.rpc.api.service.FinanceRpcService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.common.util.validate.ValidateUtil;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.PageQuery;

@Service
public class FinanceRpcServiceImpl extends BaseServiceImpl implements FinanceRpcService {
	@Autowired
	FinanceStatisticsMysqlDAO financeStatisticsMysqlDAO;

	@Autowired
	UserActMysqlDAO userActMysqlDAO;

	@Autowired
	PayFlowCollectMysqlDAO payFlowCollectMysqlDAO;

	@Autowired
	RiskTradeTypeMysqlDAO riskTradeTypeMysqlDAO;

	@Autowired
	GatewayChannelMysqlDAO gatewayChannelMysqlDAO;

	@Autowired
	PayFlowMysqlDAO payFlowMysqlDAO;
	
	@Autowired
	UserInfoService userInfoService;
	
	@Autowired
	ActService actService;
	
	@Autowired
	private DumpOrderHbaseDAO dumpOrderHbaseDAO;
	
	public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

	@Override
	@SlaveQueryAnnotation
	public List<FinanceCollectResponse> queryFinanceByTime(String accountId, Date startDate, Date endDate) {

		List<Integer> actTypes = new ArrayList<Integer>();
		actTypes.add(1);
		List<UserActDO> listPage = userActMysqlDAO.findByUserIdAndActType(accountId, actTypes);
		if (listPage == null) {
			throw new BusinessException("没有此商户,请重新输入");
		}
		String startTime = null;
		String endTime = null;
		if (startDate == null && endDate == null) {
			// SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			date.add(Calendar.DATE, -7);
			startTime = DateUtil.formatDate(date.getTime(), "yyyy-MM-dd");
		}
		if (startDate != null && endDate == null) {
			startTime = DateUtil.formatDate(startDate, "yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(startDate);
			date.add(Calendar.DATE, 31);
			endTime = DateUtil.formatDate(date.getTime(), "yyyy-MM-dd");
		}
		if (endDate != null && startDate == null) {

			Calendar date = Calendar.getInstance();
			date.setTime(endDate);
			date.add(Calendar.DATE, -31);
			startTime = DateUtil.formatDate(date.getTime(), "yyyy-MM-dd");
			endTime = DateUtil.formatDate(endDate, "yyyy-MM-dd");
		}

		if (endDate != null && startDate != null) {
			startTime = DateUtil.formatDate(startDate, "yyyy-MM-dd");
			endTime = DateUtil.formatDate(endDate, "yyyy-MM-dd");
		}
		List<FinanceCollectResponse> response = new ArrayList<FinanceCollectResponse>();
		for (FinanceTradeOrderType orderType : FinanceTradeOrderType.values()) {
			FinanceStatisticsDO financeStatisticsDO = new FinanceStatisticsDO();
			financeStatisticsDO.setTradeOrderType(orderType.getCode());
			if (orderType != FinanceTradeOrderType.TRANSFER_ORDER) {
				List<FinanceStatisticsDO> financeStatisticsDOS = financeStatisticsMysqlDAO.findStatistics(financeStatisticsDO, accountId, startTime, endTime);
				unionList(accountId, response, financeStatisticsDOS);
			} else {
				financeStatisticsDO.setPayerUserId(accountId);
				List<FinanceStatisticsDO> financeStatisticsDOS1 = financeStatisticsMysqlDAO.findStatistics(financeStatisticsDO, null, startTime, endTime);
				unionList(accountId, response, financeStatisticsDOS1);
				financeStatisticsDO.setPayerUserId(null);
				financeStatisticsDO.setPayeeUserId(accountId);
				List<FinanceStatisticsDO> financeStatisticsDOS2 = financeStatisticsMysqlDAO.findStatistics(financeStatisticsDO, null, startTime, endTime);
				unionList(accountId, response, financeStatisticsDOS2);
			}
		}
		return response;
	}

	public void unionList(String accountId, List<FinanceCollectResponse> response, List<FinanceStatisticsDO> financeStatisticsDOS) {
		for (FinanceStatisticsDO financeStatisticsDO : financeStatisticsDOS) {
			FinanceTradeOrderType tradeOrderType = FinanceTradeOrderType.valueOf(financeStatisticsDO.getTradeOrderType());

			FinanceCollectResponse res = new FinanceCollectResponse();
			res.setAccountId(accountId);
			res.setAomunt(financeStatisticsDO.getTotalAmount());
			res.setOrderCount(financeStatisticsDO.getTotalCount());
			res.setOrderStatus(financeStatisticsDO.getTradeOrderStatus());
			res.setOrderStatusDesc(FinanceOrderState.valueOf(financeStatisticsDO.getTradeOrderStatus()).getDesc());
			res.setTradeType(financeStatisticsDO.getTradeOrderType());
			res.setTradeTypeDesc(FinanceTradeOrderType.valueOf(financeStatisticsDO.getTradeOrderType()).getDesc());
			if (tradeOrderType.equals(FinanceTradeOrderType.TRANSFER_ORDER)) {
				if (financeStatisticsDO.getPayerUserId().equals(accountId)) {
					res.setTradeTypeDesc(FinanceTradeOrderType.valueOf(financeStatisticsDO.getTradeOrderType()).getDesc() + "【转出】");
					res.setAomunt(-financeStatisticsDO.getTotalAmount());
				} else if (financeStatisticsDO.getPayeeUserId().equals(accountId)) {
					res.setTradeTypeDesc(FinanceTradeOrderType.valueOf(financeStatisticsDO.getTradeOrderType()).getDesc() + "【转入】");
					// res.setAomunt(+financeStatisticsDO.getTotalAmount());
				}
			} else if (tradeOrderType.equals(FinanceTradeOrderType.REFUND_ORDER) || tradeOrderType.equals(FinanceTradeOrderType.WITHDRAW_ORDER)) {
				res.setAomunt(-financeStatisticsDO.getTotalAmount());
			}

			response.add(res);
		}
	}

	@Override
	@SlaveQueryAnnotation
	public TradeFlowListResponse queryUserTradeFlow(String accountId, String startDate, String endDate, int pageNo, int pageSize) {

		PageQuery pageQuery = null;
		if (pageNo != 0) {
			pageQuery = new PageQuery(pageNo, pageSize);
		}
		Map<String, String> bussiTypeMap = getBussiTypeMap();
		List<Integer> actTypes = new ArrayList<Integer>();
		actTypes.add(2);
		actTypes.add(4);
		actTypes.add(8);
		List<UserActDO> listPage = userActMysqlDAO.findByUserIdAndActType(accountId, actTypes);
		if (listPage == null) {
			throw new BusinessException("没有此用户,请重新输入");
		}

		String startTime = null;
		String endTime = null;
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			date.add(Calendar.DATE, -7);
			startTime = dft.format(date.getTime());
		}

		if (StringUtils.isBlank(startDate) && !StringUtils.isBlank(endDate)) {
			endTime = endDate;
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(DateUtil.parse(endDate, "yyyy-MM-dd"));
			date.add(Calendar.DATE, -31);
			startTime = dft.format(date.getTime());
		}

		if (!StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startTime = startDate;
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(DateUtil.parse(startDate, "yyyy-MM-dd"));
			date.add(Calendar.DATE, 31);
			endTime = dft.format(date.getTime());
		}

		if (!StringUtils.isBlank(startDate) && !StringUtils.isBlank(endDate)) {
			startTime = startDate;
			endTime = endDate;
		}

		TradeFlowListResponse response = getTradeFlow(accountId, startTime, endTime, null, bussiTypeMap, pageQuery);
		return response;
	}

	@Override
	@SlaveQueryAnnotation
	public TradeFlowListResponse queryPlatformTradeFlow(String accountId, String startDate, String endDate, int pageNo, int pageSize, String businessType) {
		Map<String, String> bussiTypeMap = getBussiTypeMap();
		PageQuery pageQuery = null;
		if (pageNo != 0) {
			pageQuery = new PageQuery(pageNo, pageSize);
		}
		List<Integer> actTypes = new ArrayList<Integer>();
		actTypes.add(0);
		List<UserActDO> listPage = userActMysqlDAO.findByUserIdAndActType(accountId, actTypes);
		if (listPage == null && !accountId.equals("000000")) {
			throw new BusinessException("没有此商户,请重新输入");
		}
		String startTime = null;
		String endTime = null;
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			date.add(Calendar.DATE, -7);
			startTime = dft.format(date.getTime());
		}

		if (StringUtils.isBlank(startDate) && !StringUtils.isBlank(endDate)) {
			endTime = endDate;
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(DateUtil.parse(endDate, "yyyy-MM-dd"));
			date.add(Calendar.DATE, -31);
			startTime = dft.format(date.getTime());
		}

		if (!StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startTime = startDate;
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(DateUtil.parse(startDate, "yyyy-MM-dd"));
			date.add(Calendar.DATE, 31);
			endTime = dft.format(date.getTime());
		}

		if (!StringUtils.isBlank(startDate) && !StringUtils.isBlank(endDate)) {
			startTime = startDate;
			endTime = endDate;
		}
		TradeFlowListResponse response = getTradeFlow(accountId, startTime, endTime, businessType, bussiTypeMap, pageQuery);
		return response;
	}

	private TradeFlowListResponse getTradeFlow(String accountId, String startDate, String endDate, String businessType, Map<String, String> bussiTypeMap, PageQuery pageQuery) {
		TradeFlowListResponse response = new TradeFlowListResponse();

		// 支出的
		FinanceStatisticsDO financeStatisticsOutDO = new FinanceStatisticsDO();
		financeStatisticsOutDO.setPayerUserId(accountId);
		financeStatisticsOutDO.setBussinessType(businessType);
		financeStatisticsOutDO.setTradeOrderStatus(3);
		FinanceStatisticsDO findStatisticsOut = financeStatisticsMysqlDAO.findStatisticsInAndOut(financeStatisticsOutDO, null, startDate, endDate);

		response.setCountExpendNum(findStatisticsOut == null ? 0 : findStatisticsOut.getTotalCount());
		response.setCountExpendAmount(findStatisticsOut == null ? changeF2Y(-0) : changeF2Y(-findStatisticsOut.getTotalAmount()));
		// 收入的
		FinanceStatisticsDO financeStatisticsInDO = new FinanceStatisticsDO();
		financeStatisticsInDO.setPayeeUserId(accountId);
		financeStatisticsInDO.setBussinessType(businessType);
		financeStatisticsInDO.setTradeOrderStatus(3);

		FinanceStatisticsDO findStatisticsIn = financeStatisticsMysqlDAO.findStatisticsInAndOut(financeStatisticsInDO, null, startDate, endDate);
		response.setCountIncomeNum(findStatisticsIn == null ? 0 : findStatisticsIn.getTotalCount());
		response.setCountIncomeAmount(findStatisticsIn == null ? changeF2Y(0) : changeF2Y(findStatisticsIn.getTotalAmount()));

		PayFlowCollectDO payFlowCollectDO = new PayFlowCollectDO();
		payFlowCollectDO.setBussinessType(businessType);
		payFlowCollectDO.setTradeOrderStatus(3);

		Integer findTotalCount = payFlowCollectMysqlDAO.findTotalCount(payFlowCollectDO, accountId, startDate, endDate);
		if (findTotalCount <= 0) {
			return null;
		}
		response.setTotalCount(findTotalCount);

		List<PayFlowCollectDO> findTotal = payFlowCollectMysqlDAO.findTotal(payFlowCollectDO, pageQuery, accountId, startDate, endDate);
		List<TradeFlowResponse> rows = createUserTradeFlowRows(findTotal, accountId, bussiTypeMap);
		response.setRows(rows);
		return response;
	}

	private String changeF2Y(long amount) {
		return BigDecimal.valueOf(amount).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_DOWN).toString();
	}

	private List<TradeFlowResponse> createUserTradeFlowRows(List<PayFlowCollectDO> findTotal, String accountId, Map<String, String> bussiTypeMap) {

		List<TradeFlowResponse> resp = new ArrayList<TradeFlowResponse>();

		if (!CollectionUtils.isEmpty(findTotal)) {
			for (PayFlowCollectDO payFlowCollectDO : findTotal) {
				Integer tradeOrderType = payFlowCollectDO.getTradeOrderType();
				TradeFlowResponse flowResponse = new TradeFlowResponse();
				flowResponse.setAccountId(accountId);
				if (accountId.equals(payFlowCollectDO.getPayerUserId())) {
					flowResponse.setAmount(changeF2Y(-payFlowCollectDO.getAmount()));
				} else {
					flowResponse.setAmount(changeF2Y(payFlowCollectDO.getAmount()));
				}
				flowResponse.setBussinessDesc(payFlowCollectDO.getRemark());
				flowResponse.setBussinessType(bussiTypeMap.get(payFlowCollectDO.getBussinessType()));
				flowResponse.setConfirmTime(DateFormatUtils.format(payFlowCollectDO.getConfirmTime(), "yyyy-MM-dd HH:mm:ss"));
				flowResponse.setCreateTime(DateFormatUtils.format(payFlowCollectDO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				flowResponse.setFee(changeF2Y(payFlowCollectDO.getFee()));
				flowResponse.setGatewayOrderNo(payFlowCollectDO.getGateWayNo());
				flowResponse.setPayeeTfUserId(payFlowCollectDO.getPayeeTfActNo());
				flowResponse.setPayeeUserId(payFlowCollectDO.getPayeeUserId());
				flowResponse.setPayerTfUserId(payFlowCollectDO.getPayerTfActNo());
				flowResponse.setPayerUserId(payFlowCollectDO.getPayerUserId());
				flowResponse.setTradeOrderNo(payFlowCollectDO.getPayFlowNo());
				flowResponse.setBussinessOrderNo(payFlowCollectDO.getTradeOrderNo());
				if (tradeOrderType == 1) {
					flowResponse.setTradeType("交易");
				} else if (tradeOrderType == 2) {

					if (accountId.equals(payFlowCollectDO.getPayerUserId())) {
						flowResponse.setTradeType("转账-转出");
					} else {
						flowResponse.setTradeType("转账-转入");
					}

				} else if (tradeOrderType == 3) {
					flowResponse.setTradeType("退款");
				} else if (tradeOrderType == 4) {
					flowResponse.setTradeType("提现");
				}
				resp.add(flowResponse);
			}
		}

		return resp;
	}

	// 获取全部业务类型说明bussinessType
	private Map<String, String> getBussiTypeMap() {

		Map<String, String> map = new HashMap<String, String>();
		RiskTradeTypeDO riskTradeTypeDO = new RiskTradeTypeDO();
		List<RiskTradeTypeDO> findAllBussinessType = riskTradeTypeMysqlDAO.findAllBussinessType(riskTradeTypeDO, null);
		for (RiskTradeTypeDO riskTradeTypeDO2 : findAllBussinessType) {
			map.put(riskTradeTypeDO2.getTradeTypeId(), riskTradeTypeDO2.getTradeTypeName());
		}
		return map;
	}

	@Override
	@SlaveQueryAnnotation
	public TradeOrderListResponse queryTradeOrder(TradeOrderRequest tradeOrderRequest) {
		Map<String, String> bussiTypeMap = getBussiTypeMap();
		Map<String, String> payTypeMap = getPayTypeMap();
		TradeOrderListResponse response = new TradeOrderListResponse();
		String payerUserId = tradeOrderRequest.getPayerUserId();
		String payeeUserId = tradeOrderRequest.getPayeeUserId();
		String businessType = tradeOrderRequest.getBusinessType();
		String endTime = tradeOrderRequest.getEndTime();
		int pageNo = tradeOrderRequest.getPageNo();
		int pageSize = tradeOrderRequest.getPageSize();
		String payType = tradeOrderRequest.getPayType();
		String startTime = tradeOrderRequest.getStartTime();
		Integer tradeStatus = tradeOrderRequest.getTradeStatus();
		Integer tradeType = tradeOrderRequest.getTradeType();
		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			date.set(Calendar.DATE, date.get(Calendar.DATE) - 7);
			startTime = dft.format(date.getTime());
		}
		FinanceStatisticsDO financeStatisticsOutDO = new FinanceStatisticsDO();
		financeStatisticsOutDO.setPayerUserId(payerUserId);
		financeStatisticsOutDO.setBussinessType(businessType);
		financeStatisticsOutDO.setPayeeUserId(payeeUserId);
		financeStatisticsOutDO.setTradeOrderType(tradeType);
		financeStatisticsOutDO.setBussinessType(businessType);
		financeStatisticsOutDO.setPayType(payType);
		financeStatisticsOutDO.setTradeOrderStatus(tradeStatus);
		FinanceStatisticsDO findStatisticsOut = financeStatisticsMysqlDAO.findStatisticsInAndOut(financeStatisticsOutDO, null, startTime, endTime);

		if (tradeType == 1) {
			response.setCountIncomeNum(findStatisticsOut == null ? 0 : findStatisticsOut.getTotalCount());
			response.setCountIncomeAmount(findStatisticsOut == null ? changeF2Y(0) : changeF2Y(findStatisticsOut.getTotalAmount()));
		}
		if (tradeType == 3 || tradeType == 4) {
			response.setCountExpendAmount(findStatisticsOut == null ? changeF2Y(-0) : changeF2Y(-findStatisticsOut.getTotalAmount()));
			response.setCountExpendNum(findStatisticsOut == null ? 0 : findStatisticsOut.getTotalCount());
		}
		if (tradeType == 2) {
			response.setCountIncomeNum(findStatisticsOut == null ? 0 : findStatisticsOut.getTotalCount());
			response.setCountIncomeAmount(findStatisticsOut == null ? changeF2Y(0) : changeF2Y(findStatisticsOut.getTotalAmount()));
		}

		PageQuery pageQuery = null;
		if (pageNo != 0) {
			pageQuery = new PageQuery(pageNo, pageSize);
		}

		PayFlowCollectDO payFlowCollectDO = new PayFlowCollectDO();
		payFlowCollectDO.setBussinessType(businessType);
		payFlowCollectDO.setPayerUserId(payerUserId);
		payFlowCollectDO.setPayeeUserId(payeeUserId);
		payFlowCollectDO.setPayType(payType);
		payFlowCollectDO.setTradeOrderType(tradeType);
		payFlowCollectDO.setTradeOrderStatus(tradeStatus);
		Integer findTotalCount = payFlowCollectMysqlDAO.findTotalCount(payFlowCollectDO, null, startTime, endTime);
		if (findTotalCount <= 0) {
			return null;
		}
		response.setTotalCount(findTotalCount);
		List<PayFlowCollectDO> findTotal = payFlowCollectMysqlDAO.findTotal(payFlowCollectDO, pageQuery, null, startTime, endTime);
		response.setRows(createPlatFormTradeOrders(findTotal, bussiTypeMap, payTypeMap));
		return response;
	}

	private List<TradeOrderResponse> createPlatFormTradeOrders(List<PayFlowCollectDO> findTotal, Map<String, String> bussiTypeMap, Map<String, String> payTypeMap) {

		List<TradeOrderResponse> responses = new ArrayList<TradeOrderResponse>();
		for (PayFlowCollectDO payFlowCollectDO : findTotal) {
			TradeOrderResponse orderResponse = new TradeOrderResponse();
			Integer tradeStatus = payFlowCollectDO.getTradeOrderStatus();
			Integer withDrawType = payFlowCollectDO.getWithDrawType();
			Integer tradeOrderType = payFlowCollectDO.getTradeOrderType();
			orderResponse.setAmount(changeF2Y(payFlowCollectDO.getAmount()));
			orderResponse.setBusinessRemark(payFlowCollectDO.getRemark());
			orderResponse.setBusinessType(bussiTypeMap.get(payFlowCollectDO.getBussinessType()));
			orderResponse.setCreateDate(DateFormatUtils.format(payFlowCollectDO.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			orderResponse.setFee(changeF2Y(payFlowCollectDO.getFee()));
			orderResponse.setGatewayOrderNo(payFlowCollectDO.getGateWayNo());
			orderResponse.setPayeeUserId(payFlowCollectDO.getPayeeUserId());
			orderResponse.setPayerUserId(payFlowCollectDO.getPayerUserId());
			orderResponse.setPayFlowNo(payFlowCollectDO.getPayFlowNo());
			orderResponse.setPayType(payTypeMap.get(payFlowCollectDO.getPayType()));
			orderResponse.setSuccessDate(DateFormatUtils.format(payFlowCollectDO.getConfirmTime(), "yyyy-MM-dd HH:mm:ss"));
			orderResponse.setTradeOrderNo(payFlowCollectDO.getTradeOrderNo());
			orderResponse.setBankName(payFlowCollectDO.getBankName());
			orderResponse.setBankNo(StringUtils.isEmpty(payFlowCollectDO.getBankNo()) ? null : YHBFSensitiveEnc.sensitiveDec(payFlowCollectDO.getBankNo(), "cardno"));
			if (tradeOrderType == 1) {
				orderResponse.setTradeType("支付");
			} else if (tradeOrderType == 2) {
				orderResponse.setTradeType("转账");

			} else if (tradeOrderType == 3) {
				orderResponse.setTradeType("退款");

			} else if (tradeOrderType == 4) {
				orderResponse.setTradeType("提现");
			}
			if (tradeStatus == 3) {
				orderResponse.setTradeStatus("成功");
			} else if (tradeStatus == 4) {
				orderResponse.setTradeStatus("失败");
			} else if (tradeStatus == 5) {
				orderResponse.setTradeStatus("异常");
			}
			if (withDrawType != null) {
				if (withDrawType == 2) {
					orderResponse.setWithDrawType("提现银行卡");
				} else if (withDrawType == 3) {
					orderResponse.setWithDrawType("提现支付宝");
				}
			}

			responses.add(orderResponse);
		}
		return responses;
	}

	private Map<String, String> getPayTypeMap() {
		Map<String, String> map = new HashMap<String, String>();
		GatewayChannelDO gatewayChannelDO = new GatewayChannelDO();
		List<GatewayChannelDO> listPage = gatewayChannelMysqlDAO.listPage(gatewayChannelDO, null);
		if (!CollectionUtils.isEmpty(listPage)) {
			for (GatewayChannelDO gatewayChannelDO2 : listPage) {
				map.put(gatewayChannelDO2.getChannelId(), gatewayChannelDO2.getChannelName());
			}
		}
		return map;

	}

	@Override
	@SlaveQueryAnnotation
	public List<TypeCodeNameResponse> getAllBusinessType() {
		RiskTradeTypeDO riskTradeTypeDO = new RiskTradeTypeDO();
		List<RiskTradeTypeDO> findAllBussinessType = riskTradeTypeMysqlDAO.findAllBussinessType(riskTradeTypeDO, null);
		List<TypeCodeNameResponse> response = new ArrayList<TypeCodeNameResponse>();
		if (!CollectionUtils.isEmpty(findAllBussinessType)) {
			for (RiskTradeTypeDO riskTradeTypeDO2 : findAllBussinessType) {
				TypeCodeNameResponse codeNameResponse = new TypeCodeNameResponse();
				codeNameResponse.setTypeCode(riskTradeTypeDO2.getTradeTypeId());
				codeNameResponse.setTypeName(riskTradeTypeDO2.getTradeTypeName());
				response.add(codeNameResponse);
			}
		}
		return response;
	}

	@Override
	@SlaveQueryAnnotation
	public List<TypeCodeNameResponse> getAllPayType() {
		GatewayChannelDO gatewayChannelDO = new GatewayChannelDO();
		List<GatewayChannelDO> listPage = gatewayChannelMysqlDAO.listPage(gatewayChannelDO, null);
		List<TypeCodeNameResponse> response = new ArrayList<TypeCodeNameResponse>();
		if (!CollectionUtils.isEmpty(listPage)) {
			for (GatewayChannelDO gatewayChannelDO2 : listPage) {
				TypeCodeNameResponse codeNameResponse = new TypeCodeNameResponse();
				codeNameResponse.setTypeCode(gatewayChannelDO2.getChannelId());
				codeNameResponse.setTypeName(gatewayChannelDO2.getChannelName());
				response.add(codeNameResponse);
			}
		}
		return response;
	}

	@Override
	@SlaveQueryAnnotation
	public PageModelResponse<PlatformTradeFlowResponse> queryPlatformTradeFlow(PlatformTradeFlowRequest request, Integer pageNo, Integer pageSize, Integer actType, Integer userType) {
		PageModelResponse<PlatformTradeFlowResponse> resp = new PageModelResponse<PlatformTradeFlowResponse>();
		String accountId = request.getAccountId();
		String originOrderNo = request.getOriginOrderNo();
		ValidateUtil.isNotBlank(accountId, "账号不能为空!");
		ValidateUtil.notNull(userType, "账户类型不能为空!");
		ValidateUtil.notNull(actType, "账号类型不能为空!");
		
		UserActDO findActInfo = actService.findActInfo(accountId, UserActType.valueOf(userType));
		
		if (findActInfo == null) return resp;
		
		boolean isSZTXUser = (findActInfo.getUserType().intValue() & Constance.UserType.SZTX.getCode()) != 0;
		
		if (actType == 1 && !isSZTXUser) {
			resp.setTotal(0);
			return resp;
		}
		
		if (actType == 2 && isSZTXUser) {
			resp.setTotal(0);
			return resp;
		}
		String businessType = request.getBusinessType();
		
		String endDate = "";
		String startDate = "";
		if(StringUtil.isBlank(originOrderNo)){
			createQueryTime(request);
			endDate = request.getEndDate();
			startDate = request.getStartDate();
		}
		
		
		int total = payFlowMysqlDAO.queryPlatformTradeFlowCount(originOrderNo,findActInfo.getActNo(), businessType, endDate, startDate, userType);
		
		resp.setTotal(total);
		if (total < 1)
			return resp;
		if (pageSize == null) {
			pageNo = null;
		} else {
			pageNo = (pageNo - 1) * pageSize;
		}
		List<PlatformTradeFlowDO> rows = payFlowMysqlDAO.queryPlatformTradeFlow(originOrderNo,findActInfo.getActNo(), businessType, endDate, startDate, pageNo, pageSize, userType);
		resp.setList(createTradeFlowList(rows));
		return resp;
	}

	private List<PlatformTradeFlowResponse> createTradeFlowList(List<PlatformTradeFlowDO> rows) {
		List<PlatformTradeFlowResponse> resp = new ArrayList<PlatformTradeFlowResponse>();
		for (PlatformTradeFlowDO platformTradeFlowDO : rows) {
			PlatformTradeFlowResponse response = new PlatformTradeFlowResponse();
			String payeeUserId = platformTradeFlowDO.getPayeeUserId();
			String payerUserId = platformTradeFlowDO.getPayerUserId();
			String accountId = platformTradeFlowDO.getAccountId();
			Integer payerActType = platformTradeFlowDO.getPayerActType();
			Integer payeeActType = platformTradeFlowDO.getPayeeActType();
			long tradeAmount = platformTradeFlowDO.getTradeAmount();
			int tradeType = platformTradeFlowDO.getTradeType();
			Integer payFlowType = platformTradeFlowDO.getPayFlowType();
			String payFlowInnerRemark = platformTradeFlowDO.getPayFlowInnerRemark();
			BeanUtils.copyProperties(platformTradeFlowDO, response);
			response.setTradeTypeDesc(TradeOrderType.valueOf(response.getTradeType()).getDesc());
			if (TradeOrderType.B2C_TRADE_ORDER.getCode() == tradeType) {
				if (PayFlowType.PAY.getCode() == payFlowType) {
					response.setPayerUserId(Constance.USER_SZTX);
					response.setPayerActType(Constance.UserActType.CASH.getCode());
					response.setPayeeUserId(payeeUserId);
					response.setPayeeActType(payeeActType);
				} else {
					response.setPayerUserId(payerUserId);
					response.setPayerActType(payerActType);
					response.setPayeeUserId(payeeUserId);
					response.setPayeeActType(payeeActType);
				}
			}
			
			if (TradeOrderType.CHARGE_ORDER.getCode() == tradeType  ) {
				response.setPayerUserId(Constance.USER_SZTX);
				response.setPayerActType(Constance.UserActType.CASH.getCode());
				response.setPayeeUserId(payeeUserId);
				response.setPayeeActType(payeeActType);
			}
			if (TradeOrderType.SETTLE_ORDER.getCode() == tradeType || TradeOrderType.SETTLE_PROFIT.getCode() == tradeType || TradeOrderType.SETTLE_DISTRIBUTOR.getCode() == tradeType || TradeOrderType.TRANSFER_ORDER.getCode() == tradeType || TradeOrderType.BIND_CARD_ORDER.getCode() == tradeType || TradeOrderType.BALANCE_TRANSFER.getCode() == tradeType) {
				response.setPayerUserId(payerUserId);
				response.setPayerActType(payerActType);
				response.setPayeeUserId(payeeUserId);
				response.setPayeeActType(payeeActType);
			}
			
			if (TradeOrderType.BALANCE_WITHDRAW_ORDER.getCode() == tradeType || TradeOrderType.PROFIT_WITHDRAW_ORDER.getCode() == tradeType ) {
				response.setPayerUserId(payerUserId);
				response.setPayerActType(payerActType);
				response.setPayeeUserId(Constance.USER_SZTX);
				response.setPayeeActType(Constance.UserActType.CASH.getCode());
			}
			
			if (TradeOrderType.REFUND_ORDER.getCode() == tradeType) {
				response.setPayerUserId(payerUserId);
				response.setPayerActType(payerActType);
				if (StringUtils.isNotBlank(payFlowInnerRemark) && payFlowInnerRemark.startsWith("3")) {
					response.setPayeeUserId(Constance.USER_SZTX);
					response.setPayeeActType(Constance.UserActType.CASH.getCode());
				} else {
					response.setPayeeUserId(payeeUserId);
					response.setPayeeActType(payeeActType);
				}
			}
			
			if (TradeOrderType.TRADE_FEE.getCode() == tradeType) {
				response.setPayerUserId(payerUserId);
				if  (payerActType == null) {
					response.setPayerActType(Constance.UserActType.CASH.getCode());
				} else {
					response.setPayerActType(payerActType);
				}
				response.setPayeeUserId(Constance.USER_PROFIT);
				response.setPayeeActType(Constance.UserActType.CASH.getCode());
			}
			
			resp.add(response);
		}
		return resp;
	}

	private void createQueryTime(PlatformTradeFlowRequest request) {
		String startDate = request.getStartDate();
		String endDate = request.getEndDate();

		String startTime = null;
		String endTime = null;
		if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(new Date());
			date.add(Calendar.DATE, -7);
			startTime = dft.format(date.getTime());
			endTime = dft.format(Calendar.getInstance().getTime());
		}

		if (StringUtils.isBlank(startDate) && !StringUtils.isBlank(endDate)) {
			endTime = endDate;
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(DateUtil.parse(endDate, "yyyy-MM-dd"));
			date.add(Calendar.DATE, -31);
			startTime = dft.format(date.getTime());
		}

		if (!StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
			startTime = startDate;
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Calendar date = Calendar.getInstance();
			date.setTime(DateUtil.parse(startDate, "yyyy-MM-dd"));
			date.add(Calendar.DATE, 31);
			endTime = dft.format(date.getTime());
		}

		if (!StringUtils.isBlank(startDate) && !StringUtils.isBlank(endDate)) {
			startTime = startDate;
			endTime = endDate;
		}
		
		request.setStartDate(startTime + " 00:00:00");
		request.setEndDate(endTime + " 23:59:59");
	}

	@Override
	public PlatformTradeFlowHbaseResp queryTradeFlowByHbase(PlatformTradeFlowHbaseRequest request, Integer pageSize, Integer actType, Integer userType) {
		PlatformTradeFlowHbaseResp resp = new PlatformTradeFlowHbaseResp();
		String accountId = request.getAccountId();
		String originOrderNo = request.getOriginOrderNo();
		ValidateUtil.isNotBlank(accountId, "账号不能为空!");
		ValidateUtil.notNull(actType, "账号类型不能为空!");
		ValidateUtil.notNull(userType, "账户类型不能为空!");
		
		UserActDO findActInfo = actService.findActInfo(accountId, UserActType.valueOf(userType));
		
		if (findActInfo == null) return resp;
		
		boolean isSZTXUser = (findActInfo.getUserType().intValue() & Constance.UserType.SZTX.getCode()) != 0;
		
		if (actType == 1 && !isSZTXUser) {
			return resp;
		}
		
		if (actType == 2 && isSZTXUser) {
			return resp;
		}
		
		String businessType = request.getBusinessType();
		
		String endDate = "";
		String startDate = "";
		if (StringUtil.isBlank(originOrderNo)) {
			if (StringUtils.isNotBlank(request.getEndDate())) {
				endDate = request.getEndDate() + " 23:59:59";
			}
			if (StringUtils.isNotBlank(request.getStartDate())) {
				startDate = request.getStartDate() + " 00:00:00";
			}
		}
//		List<PlatformTradeFlowDO> rows = payFlowMysqlDAO.queryPlatformTradeFlow(originOrderNo,findActInfo.getActNo(), businessType, endDate, startDate, pageNo, pageSize, userType);
		HbaseQueryDO hbaseQueryDO = dumpOrderHbaseDAO.scanCmsActFlow(request.getNextPageStartRowKey(), originOrderNo, startDate, endDate, accountId, userType, businessType, pageSize);
		List<String> jsonDataList = hbaseQueryDO.getJsonDataList();
		if (CollectionUtils.isEmpty(jsonDataList)) return resp;
		List<PlatformTradeFlowResponse> rows = new ArrayList<PlatformTradeFlowResponse>();
		for (String string : jsonDataList) {
			UserActFlowDumpDO dumpDO = JSONObject.parseObject(string, UserActFlowDumpDO.class);
			PlatformTradeFlowResponse response = new PlatformTradeFlowResponse();
			response.setAccountId(accountId);
			response.setBanlance(dumpDO.getActBalance());
			response.setBussinessDesc(dumpDO.getBusinessDesc());
			response.setBussinessType(dumpDO.getBusinessType());
			response.setModifyCount(dumpDO.getModifyCount());
			response.setOtherUserId(dumpDO.getOtherUserId());
			response.setPayeeActType(dumpDO.getPayeeActType());
			response.setPayeeUserId(dumpDO.getPayeeUserId());
			response.setPayerActType(dumpDO.getPayerActType());
			response.setPayerUserId(dumpDO.getPayerUserId());
			response.setTradeAmount(dumpDO.getTranAmount());
			response.setTradeOrderNo(dumpDO.getOriginOrderNo());
			response.setTradeTime(dumpDO.getTradeTime());
			response.setCreateTime(dumpDO.getCreateTime());
			response.setTradeType(dumpDO.getTradeType());
			response.setTradeTypeDesc(dumpDO.getTradeTypeDesc());
			response.setTradeFlowOrderNo(dumpDO.getTradeOrderNo());
			rows.add(response);
		}
		resp.setHasNextPage(hbaseQueryDO.getHasNextPage());
		resp.setNextPageStartRowKey(hbaseQueryDO.getNextPageStratRowKey());
		resp.setRows(rows);
		return resp;
	}
}
